/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.safetytask.controller;

import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.safetytask.domain.CustomForm;
import com.koron.safetytask.service.api.CustomFormService;
import com.koron.safetytask.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "customForm", description = "自定义表单接口类")
@RestController
@RequestMapping("/customForm")
public class CustomFormController {

    @Autowired
    private CustomFormService customFormService;

    @ApiOperation("批量新增自定义表单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "自定义表名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态，0-草稿，1-已生效", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "customFormList", value = "自定义表单集合", required = true, paramType = "query", dataType = "String")
    }
    )
    @RequestMapping(value = "/batchInsert.htm", method = RequestMethod.POST)
    @Log(title = "批量新增自定义表单", businessModule = "自定义表单", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<Integer> batchInsert(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                            @RequestBody CustomForm customForm) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        List<CustomForm> customFormList = customForm.getCustomFormList();
        String name = customForm.getName();
        Integer state = customForm.getState();
        if (StringUtils.isEmpty(name)) {
            MessageBean<Integer> msg = new MessageBean<>();
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription("自定义表单名不能为空");
            return msg;
        }
        return customFormService.batchInsert(customFormList, name, state, user);
    }


    @ApiOperation("批量修改自定义表单接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "自定义表名", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态，状态，0-草稿，1-已生效", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "customFormList", value = "自定义表单", required = true, paramType = "query", dataType = "String")
    }
    )
    @RequestMapping(value = "/batchUpdate.htm", method = RequestMethod.POST)
    @Log(title = "批量修改自定义表单", businessModule = "自定义表单", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<Integer> batchUpdate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                            @RequestBody CustomForm customForm) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        List<CustomForm> customFormList = customForm.getCustomFormList();
        String name = customForm.getName();
        Integer state = customForm.getState();
        if (StringUtils.isEmpty(name)) {
            MessageBean<Integer> msg = new MessageBean<>();
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription("自定义表单名不能为空");
            return msg;
        }
        return customFormService.batchUpdate(customFormList, name, state, user);
    }

    /**
     * 根据id批量删除表单
     *
     * @param user
     * @param ids
     * @return
     */
    @ApiOperation("批量删除自定义表单项")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "表单id，多个之间用逗号分开", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/batchDelete.htm", method = RequestMethod.POST)
    @Log(title = "批量删除自定义表单项", businessModule = "自定义表单", businessManipulation = BusinessManipulation.DELETE)

    public MessageBean<Integer> batchDelete(@SPIAccountAnno @StaffAttribute("_user")
                                            @ApiIgnore StaffBean user, String ids) {
        if (null == user) {
            MessageBean<Integer> messageBean = new MessageBean<>();
            messageBean.setCode(BusinessCode.NOT_LOGIN.getCode());
            messageBean.setDescription(BusinessCode.NOT_LOGIN.getDescription());
            return messageBean;
        }
        List<String> idList = new ArrayList<>();
        if (StringUtils.isNotEmpty(ids)) {
            String[] idArray = ids.split("[,，]");
            idList = Arrays.asList(idArray);
        }
        return customFormService.batchDelete(idList);
    }

    /**
     * 根据表单编码删除表单
     *
     * @param code
     * @return
     */
    @ApiOperation("删除自定义表单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", required = true, dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    @Log(title = "删除自定义表单", businessModule = "自定义表单", businessManipulation = BusinessManipulation.DELETE)

    public MessageBean<Integer> delete(@RequestParam final String code) {
        return customFormService.delete(code);
    }

    @ApiOperation("根据自定义表单编码和添加人查询表单详情接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "表单编码", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "addUser", value = "添加人code" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "addUserName", value = "添加人Name" , dataType = "String", paramType = "query")
    })
    @RequestMapping(value = "/details.htm", method = RequestMethod.GET)
    public MessageBean<CustomForm> details(@RequestParam final String code, @RequestParam(required = false) final String addUser, @RequestParam(required = false) final String addUserName) {
        MessageBean<CustomForm> listMessageBean = customFormService.queryByCode(code, addUser, addUserName);
        return listMessageBean;
    }


    @ApiOperation("获取所有的自定义表单接口")
    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "表格名称" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "addUserName", value = "添加人name" , dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "int", paramType = "query")
    })
    public MessageBean<DataBean<CustomForm>> list(@RequestParam(required = false) String name, @RequestParam(required = false) String addUserName, @RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize) {
        Integer start = null;
        if (page == null || page <= 0) {
            page = 1;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 15;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        MessageBean<DataBean<CustomForm>> messageBean = customFormService.list(name,addUserName,start, pageSize);
        if (null != messageBean.getData()) {
            messageBean.getData().setPageNo(page);
        }
        return messageBean;
    }


    @ApiOperation("获取所有可使用的自定义表单接口")
    @RequestMapping(value = "/availableFormsList.htm", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "int", paramType = "query")
    })
    public MessageBean<DataBean<CustomForm>> availableFormsList(@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer pageSize) {
        Integer start = null;
        if (page == null || page <= 0) {
            page = 0;
            if (pageSize == null || pageSize <= 0) {
                pageSize = 0;
            }
        }
        start = (page <= 1) ? 0 : (page - 1) * pageSize;
        MessageBean<DataBean<CustomForm>> messageBean = customFormService.availableFormsList(start, pageSize);
        if (null != messageBean.getData()) {
            messageBean.getData().setPageNo(page);
        }
        return messageBean;
    }
}
